//
//  SinaTabBarViewController.swift
//  WeiBo
//
//  Created by Apple on 15/12/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit


/*
    
*/
class SinaTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
       
        
        //添加子控制器
        addChildViewController()
    
        // 改变系统tabbar
        let tabbar = SinaTabBar()
        // tabBar 只读的
//        tabBar = tabbar
        // 用KVC给 只读属性赋值
        setValue(tabbar, forKeyPath: "tabBar")
        
        // 
        tabbar.composeButton.addTarget(self, action: "publish", forControlEvents: UIControlEvents.TouchUpInside)
        
        
    }
    
    //MARK:--发布微博按钮点击
    func publish(){
    
        printLog("发布界面")
        
        let publish = SinaPublishViewController()
        
        let nav = UINavigationController(rootViewController: publish)
        
        presentViewController(nav, animated: true) { () -> Void in
            
        }
    }

    //
    func addChildViewController() {
        addChildViewController(SinaHomeViewController(), title: "首页", image: "tabbar_home")
        addChildViewController(SinaMessageViewController(), title: "消息", image: "tabbar_message_center")
        addChildViewController(SinaFindViewController(), title: "发现", image: "tabbar_discover")
        addChildViewController(SinaSettingViewController(), title: "设置", image: "tabbar_profile")
        
//        addChildViewController(addChildViewController(nil , title: "aaa", image: "tabbar_profile"))
    }
    //MARK: -- 抽象封装一个方法 -- 重载
    func addChildViewController(childController: UIViewController?,title: String,image: String) {
        
//        if childController == nil {
//        
//            fatalError("你 的控制为空,我不能让你继续走下去了,你得改这个bug")
//        }
        
        childController?.title = title
        
        //设置图片
        childController?.tabBarItem.image = UIImage(named: image)
        // 设置选中的
        childController?.tabBarItem.selectedImage = UIImage(named: "\(image)_highlighted")
        // 设置nav
        let nav = UINavigationController(rootViewController: childController!)
        // 添加到控制器里
        addChildViewController(nav)
        
    }
   

}
